package com.openingedu.mba.screen.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lurencun.android.support.widget.CommonAdapter;
import com.openingedu.mba.R;
import com.openingedu.mba.entity.PaperVO;
import com.openingedu.mba.persist.DbOrder;
import com.openingedu.mba.service.OrderService;

public class ServerLogicAdapter extends CommonAdapter<PaperVO>{

	private Context mContext;
	
	private OrderService orderService;
	final Handler handler;
	
	
	public ServerLogicAdapter(Context context, Handler handler) {
		super(context);
		mContext = context;
		this.handler = handler;
		orderService = new OrderService();
	}

	@Override
	protected View createView(LayoutInflater inflater, final PaperVO data,
			int position, View convertView, ViewGroup parent) {
		LinearLayout itemLayout = (LinearLayout) inflater.inflate(R.layout.item_server_logic, null);
		TextView questionNumView = (TextView) itemLayout.findViewById(R.id.question_num_tv);
		// 试题名称
		TextView paperTitleView = (TextView) itemLayout.findViewById(R.id.paper_title);
		TextView paperPriceView = (TextView) itemLayout.findViewById(R.id.paper_price);
		paperTitleView.setText(data.getPaperTitle());
		questionNumView.setText("" + data.getQuestionNum());
		// 购买按钮
		Button buyBtn = (Button) itemLayout.findViewById(R.id.buyBtn);
		// 下载按钮
		Button downBtn = (Button) itemLayout.findViewById(R.id.downBtn);
		
		buyBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				DbOrder order = orderService.findNonePaidOrder(mContext, data.getPaperID());
				if (order != null) {
					Toast.makeText(mContext, "已经存在订单，请在[订单管理]中支付即可", Toast.LENGTH_LONG).show();
				}
				else {
					Message msg = Message.obtain(handler, 1, data);
					handler.sendMessage(msg);
				}
			}
		});
		
		downBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Message msg = Message.obtain(handler, 0, data);
				handler.sendMessage(msg);
			}
		});
		
		Double paperPrice = data.getPaperPrice();
		// 免费试卷
		if (paperPrice == 0) {
			// 已经下载过
			if (data.isDownloaded()) {
				downBtn.setVisibility(View.INVISIBLE);
				buyBtn.setVisibility(View.GONE);
			}
			// 没有下载过
			else {
				downBtn.setVisibility(View.VISIBLE);
				buyBtn.setVisibility(View.GONE);
			}
		}
		// 付费试卷
		else {
			paperPriceView.setBackgroundResource(R.drawable.orange_background);
			paperPriceView.setText(paperPrice + "RMB");
			// 已经购买过,显示下载按钮
			if (data.isPaid()) {
				// 已经下载过
				if (data.isDownloaded()) {
					downBtn.setVisibility(View.INVISIBLE);
					buyBtn.setVisibility(View.GONE);
				}
				// 没有下载过
				else {
					downBtn.setVisibility(View.VISIBLE);
					buyBtn.setVisibility(View.GONE);
				}
			}
			// 没有购买过，显示购买按钮
			else {
				buyBtn.setVisibility(View.VISIBLE);
				downBtn.setVisibility(View.GONE);
			}
		}
		return itemLayout;
	}

}
